package com.onlinevideo.servlet;

import cn.hutool.json.JSONUtil;
import com.onlinevideo.data.ResultBean;
import com.onlinevideo.entity.Role;
import com.onlinevideo.service.RoleService;
import com.onlinevideo.service.impl.RoleServiceImpl;
import com.onlinevideo.util.MyBeanUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class RoleServlet  extends  BaseServlet{
    private PrintWriter writer = null;
    private  RoleService roleService = new RoleServiceImpl();
    protected void insert(HttpServletRequest req, HttpServletResponse resp) throws IOException, IllegalAccessException, InstantiationException, ClassNotFoundException {
       writer =resp.getWriter();
        Role role = MyBeanUtils.set(Role.class, req.getParameterMap());
        int result = roleService.insert(role);
        if(result>0){
            writer.write(JSONUtil.toJsonStr(ResultBean.scOperationSuccess(role.getRoleName()+"新增成功")));
        }else {
            writer.write(JSONUtil.toJsonStr(ResultBean.scOperationError(role.getRoleName()+"新增失败")));
        }
    }

    protected void update(HttpServletRequest req, HttpServletResponse resp) throws IOException, IllegalAccessException, InstantiationException, ClassNotFoundException {
        writer = resp.getWriter();
        Role role = MyBeanUtils.set(Role.class, req.getParameterMap());
        int result = roleService.update(role);
        if(result>0){
            writer.write(JSONUtil.toJsonStr(ResultBean.scOperationSuccess(role.getId()+"更新成功")));
        }else {
            writer.write(JSONUtil.toJsonStr(ResultBean.scOperationError(role.getId()+"更新失败")));
        }
    }

    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException, IllegalAccessException, InstantiationException, ClassNotFoundException {
        writer = resp.getWriter();
        Role role = MyBeanUtils.set(Role.class, req.getParameterMap());
        int result = roleService.delete(role.getId());
        if(result>0){
            writer.write(JSONUtil.toJsonStr(ResultBean.scOperationSuccess(role.getId()+"删除成功")));
        }else {
            writer.write(JSONUtil.toJsonStr(ResultBean.scOperationError(role.getId()+"删除失败")));
        }
    }

    protected void queryAll(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        writer = resp.getWriter();
        List<Role> roles = roleService.queryAll();
        req.setAttribute("roles",roles);
        if(roles.size()>0){
            writer.write(JSONUtil.toJsonStr(ResultBean.scOperationSuccess("查询成功",roles)));
        }else {
            writer.write(JSONUtil.toJsonStr(ResultBean.scOperationError("数据为空")));
        }
        try {
            req.getRequestDispatcher("../gateway?action=rolelist").forward(req, resp);
        } catch (ServletException e) {
            e.printStackTrace();
        }
    }

    protected void queryById(HttpServletRequest req, HttpServletResponse resp) throws IOException, IllegalAccessException, InstantiationException, ClassNotFoundException {
        writer = resp.getWriter();
        Role role = MyBeanUtils.set(Role.class, req.getParameterMap());
        Role role1 = roleService.queryById(role.getId());
        if(role1 == null){
            writer.write(JSONUtil.toJsonStr(ResultBean.scOperationError(role.getId() + "不存在")));
            return;
        }
        writer.write(JSONUtil.toJsonStr(ResultBean.scOperationSuccess("查询成功",role1)));
    }
}
